package zhao.ben.quan.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import zhao.ben.quan.constant.MessageConstant;
import zhao.ben.quan.entity.PageResult;
import zhao.ben.quan.entity.QueryPageBean;
import zhao.ben.quan.entity.Result;
import zhao.ben.quan.pojo.CheckGroup;
import zhao.ben.quan.pojo.CheckItem;
import zhao.ben.quan.service.CheckGroupService;

import java.util.List;

/**
 * 检查组管理
 */
@RestController//标注这是一个controller层和返回值不经过视图解析 相当于@ResponseBody+@Controller
@RequestMapping("/checkgroup")

public class CheckGroupController {

    //注入接口对象
    @Reference//选择dubbo下的
    private CheckGroupService checkGroupService;

    //新增检查组
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            //新增成功
            checkGroupService.add(checkGroup, checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            //新增失败
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = checkGroupService.findPage(queryPageBean);

        return pageResult;
    }


    //编辑回显检查组
    @RequestMapping("/findByid")
    public Result findByid(Integer id) {
        try {
            //回显查询检查组成功
            CheckGroup checkGroup = checkGroupService.findByid(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        } catch (Exception e) {
            //回显查询检查组失败
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


    //编辑回显检查项信息:根据检查组id查询
    @RequestMapping("/findCheckitemIdsByCheckGroupId")//findCheckitemIdsByCheckGroupId
    public Result findCheckitemIdsByCheckGroupId(Integer id) {
        try {
            //回显查询检查组成功
            List<Integer> checkitemIds = checkGroupService.findCheckitemIdsByCheckGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemIds);
        } catch (Exception e) {
            //回显查询检查组失败
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    //提交编辑数据
    @RequestMapping("/edit")//findCheckitemIdsByCheckGroupId
    public Result edit(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds) {
        try {
            //回显查询检查组成功
             checkGroupService.edit(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS, checkitemIds);
        } catch (Exception e) {
            //回显查询检查组失败
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }


    //新增方法
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<CheckGroup> checkGroups = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
